﻿using System.Collections;
using UnityEngine;
using Zenject;

public class EnemyAttackAction : FSMActionMB
{
    private EnemyBase enemy;
    private Rigidbody2D rigidbody2D;
    private bool canAttack = true;
    [Inject] private GameManager _gameManager;


    private void Awake()
    {
        enemy = GetComponent<EnemyBase>();
        rigidbody2D = GetComponent<Rigidbody2D>();
    }

    public override void Act()
    {
        // 每帧判断
        if (_gameManager.currentPlayer == null ||
            _gameManager.currentPlayer.GetComponent<PlayerHealth>().isDead) return;
        ActAttack();
    }

    private void ActAttack()
    {
        if (canAttack)
        {
            // TODO 计算伤害值
            // Debug.Log("Enemy took " + enemy.damage + " damage");
            _gameManager.currentPlayer.GetComponent<PlayerHealth>().TakeDamage(enemy.damage);
            StartCoroutine(AttackCooldown());
        }
    }

    IEnumerator AttackCooldown()
    {
        canAttack = false;
        yield return new WaitForSeconds(enemy.attackCoolTime);
        canAttack = true;
    }

    public override void Reset()
    {
    }
}